var old = null;
var times = 0;
var internal = 2000;
var limit = -1;
var charts_tps = null;
var charts_clients = null;
jQuery(function($) {
	Highcharts.setOptions({
		global : {
			useUTC : false
		}
	});
	charts_tps = drawChartsTPS();
	charts_clients = drawChartsClients();
	get();
});
function get(){
	$.get(window.global.getFinalUrl(window.global.ctx+"/monitor/redis"), function(result) {
		if(result.status==200){
			var data = result.data.info;
			var now = (new Date()).getTime();
//			console.log(charts_clients+":"+Highcharts.dateFormat('%H:%M:%S',now))
			charts_clients.series[0].addPoint([ now, parseInt(data.connected_clients) ], true, true);
			charts_tps.series[0].addPoint([ now, parseInt(data.instantaneous_ops_per_sec) ], true, true);
			if(old != null){
				var cha = (parseInt(data.dbSize) - parseInt(old.dbSize)) ;
				var dbsize_rate = Math.abs(cha) / (old.dbSize==0?1:old.dbSize);
				setRate("dbsize_rate",dbsize_rate,cha);
				
				var hit_rate = data.keyspace_hits / (parseInt(data.keyspace_hits) + parseInt(data.keyspace_misses));
				var cha = (hit_rate - old.hit_rate) ;
				setRate("hit_rate_rate",Math.abs(cha),cha);
				
				var cha = (parseInt(data.used_memory) - parseInt(old.used_memory)) ;
				var dbsize_rate = Math.abs(cha) / (old.used_memory==0?1:old.used_memory);
				setRate("used_memory_human_rate",dbsize_rate,cha);
				
				var cha = (parseInt(data.blocked_clients) - parseInt(old.blocked_clients)) ;
				var dbsize_rate = Math.abs(cha) / (old.blocked_clients==0?1:old.blocked_clients);
				setRate("blocked_clients_rate",dbsize_rate,cha);
			}
			$("#dbsize").text(data.dbSize);
			$("#used_memory_human").text(data.used_memory_human);
			$("#blocked_clients").text(data.blocked_clients);
			var hit_rate = data.keyspace_hits / (parseInt(data.keyspace_hits) + parseInt(data.keyspace_misses));
			if(old==null){
				old = data;
				old.hit_rate=hit_rate;
			}
			$("#hit_rate").text((hit_rate * 100).toFixed(1));
			times++;
			if(times==1){
				pageInit();
			}
			if((limit>0 && times<=limit) || limit<0){
				if(start){
					setTimeout(function(){
						get();
					},internal);
				}
			}
		}
	});
	pageInit();
}
function setRate(id,rate,cha){
//	console.log(id+":"+rate+":"+cha);
	var target = $("#"+id);
	var stat = target.closest(".stat");
	if(cha<0){
		stat.removeClass("stat-success");
		if(!stat.hasClass("stat-important")){
			stat.addClass("stat-important");
		} 
	}else{
		stat.removeClass("stat-important");
		if(!stat.hasClass("stat-success")){
			stat.addClass("stat-success");
		} 
	}
	target.text((rate * 100).toFixed(1)+"%");
}
function drawChartsClients() {
	var points = 7;
	return  new Highchart("#charts_clients",{
        chart: {
            type: 'spline'
        },
		title : {
			text : 'Clients'
		},
		xAxis : {
			type : 'datetime',
			dateTimeLabelFormats: {   
				second: '%H:%M:%S' 
            }
		   ,tickInterval:internal
		   ,tickLength:points
		},
		yAxis : {
			title : {
				text : '连接数'
			},
			min : 0,
			allowDecimals : false,
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
		}, 
		tooltip : {
			formatter : function() {
				return "时间: " + Highcharts.dateFormat('%H:%M:%S', this.x) + "<br/>连接数: " + this.y;
			},
			crosshairs : true
		},
		legend : {
			enabled : false
		},
		exporting : {
			enabled : false
		},
		series : [ {
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		} ]
	}).getTarget();
}
function drawChartsTPS() {
	var points = 7;
	return new Highchart("#charts_tps",{
        chart: {
            type: 'spline'
        },
		title : {
			text : 'TPS'
		},
		xAxis : {
			type : 'datetime',
			dateTimeLabelFormats: {   
				second: '%H:%M:%S' 
            }
		   ,tickInterval:internal
		   ,tickLength:points
		},
		yAxis : {
			title : {
				text : '执行命令数'
			},
			min : 0,
			allowDecimals : false,
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
		},
		tooltip : {
			formatter : function() {
				return "时间: " + Highcharts.dateFormat('%H:%M:%S', this.x) + "<br/> TPS: " + this.y;
			},
			crosshairs : true
		},
		legend : {
			enabled : false
		},
		exporting : {
			enabled : false
		}, 
		series : [ {
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		} ]
	}).getTarget();
}